#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys, getopt

opts, args = getopt.getopt(sys.argv[1:], "hc:")
httpdconf_file="" #apache 配置文件路径
for op, value in opts:
    if op == "-c":
        httpdconf_file = value
    elif op == "-h":
        usage()
        sys.exit()

fd = open(httpdconf_file, 'r+') 
content=fd.read();#读取配置内容
fd.close();


fw = open(httpdconf_file, 'w+') 
#处理写入内容
# 配置/usr/local/apache2/conf/httpd.conf
# 找到：    
# AddType  application/x-compress .Z    
# AddType application/x-gzip .gz .tgz    
# 在后面添加：    
# AddType application/x-httpd-php .php（使Apcche支持PHP）    
# AddType application/x-httpd-php-source .phps      
# 找到：    <IfModule dir_module>    DirectoryIndex index.html    </IfModule>    
# 添加：    <IfModule dir_module>    DirectoryIndex index.html index.php    </IfModule>        
# 找到：    ＃ServerName www.example.com:80    
# 修改为：  ServerName 127.0.0.1:80或者ServerName localhost:80    记得要去掉前面的“＃”  
# 修改默认的Web站点目录
# 找到：DocumentRoot "/usr/local/apache2/htdocs"
# 修改为：DocumentRoot "/home/www/WebSite" --该目录为自己创建的目录
# 找到：<Directory "/usr/local/apache2/htdocs"> 
# 修改为：<Directory "/home/www/WebSite">
str1="AddType application/x-gzip .gz .tgz"
str1_r='''AddType application/x-gzip .gz .tgz                       
    AddType application/x-httpd-php .php  
    AddType application/x-httpd-php-source .phps'''
content=content.replace(str1,str1_r)

str2="DirectoryIndex index.html"
str2_r="DirectoryIndex index.html index.php"
content=content.replace(str2,str2_r)

str3="#ServerName www.example.com:80"
str3_r='''#ServerName www.example.com:80
    ServerName 127.0.0.1:80
'''
content=content.replace(str3,str3_r)

str4="#DocumentRoot \"/var/local/mylamp/apache/httpd//htdocs\""
str4_r="#DocumentRoot \"/var/local/mylamp/www\""
content=content.replace(str4,str4_r)

str5="/var/local/mylamp/apache/httpd//htdocs"
str5_r="/var/local/mylamp/www"
content=content.replace(str5,str5_r)

fw.write(content)
fw.close()
